<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 低配实现 & 调用流程分析 generator

        function gen$(_context) {
            console.log('_context ===> ', _context)
            while (1) {
                switch (_context.prv = _context.next) {
                    case 0:
                        _context.next = 2
                        return 'result 1'
                    case 2:
                        _context.next = 4
                        return 'result 2'
                    case 4:
                        _context.next = 6
                        return 'result 3'
                    case 6:
                    case "end":
                        return _context.stop();
                }
            }
        }

        // 低配版context
        var context = {
            next: 0,
            prev: 0,
            done: false,
            stop() {
                // 结束迭代
                this.done = true
            }
        }

        // 低配版 invoke
        let gen = function () {
            return {
                next() {
                    value = context.done ? 'undefined' : gen$(context)
                    done = context.done
                    return {
                        value, done
                    }
                }
            }
        }

        var g = gen()
        debugger
        console.log(g.next())
        console.log(g.next())
        console.log(g.next())
        console.log(g.next())
    </script>
</body>

</html>